import apiMixin from '@/mixin/apiMixin';
import taskMixin from '@/mixin/taskMixin';
import fetch from '@/api/axios';
import utils from '@/utils/index';
import envConfig from '@/config/env.js';

export default {
    mixins: [apiMixin,taskMixin],
    data() {
        return {
            authorized_key:  utils.handleLocalStorage.get('authorized_key'),
            host: envConfig.baseUrl,
            tableData:[],
            classShow:false,
            loading: false,
            listNumsTotal: 0,
            bid: 0,
            curIndex: 0,
            editObj: {},
            searchText: '',
            // edit 修改 add 新增
            handleType: 'edit',
            curPage: 1,

            aliasShow:false,
            aliaLoading:false,
            dynamicTags: [],
            inputVisible: false,
            inputValue: ''
        }
    },
    mounted() {
        this.init();
    },
    methods: {
        getTaskSubjectListApi(page) {
            let _self = this;
            this.comResquest(this.request.getTaskSubjectList,(res)=>{
                _self.curPage = Number(page) || 1;
                _self.updateRoutePar(page);
                let { count,result } = res;
                _self.listNumsTotal = count;
                _self.tableData = result;
            },{
                page,
                keyword: this.searchText,
                load: true
            })
        },
        changePage(page){
            page = page || 1;
            this.getTaskSubjectListApi(page);
        },
        init() {
            this.getTaskSubjectListApi(this.curPage);
        },
        createBid() {
            let prefix = utils.getUploadPrefix();
            let bid = prefix + '-' +  utils.createBidFn();
            this.bid = bid;
            return  bid;
        },
        postEditTaskSubjectApi() {
            let _self = this;
            let changeObj = null;
            if (this.handleType === 'edit') {
                changeObj = utils.simpleCopy(this.tableData[this.curIndex])
            }else{
                changeObj = utils.simpleCopy(this.editObj)
            }
            changeObj.bid = this.bid;
            this.comResquest(this.request.postEditTaskSubject,(res)=>{
                _self.initPopPar();
                _self.init();
                _self.$message({
                    message: '操作成功',
                    type: 'success'
                });
            },{
                update_data: changeObj,
                load: true,
            })
        },
        handlePopShow() {
            this.classShow = !this.classShow;
        },
        addSubject() {
            this.handleType = 'add';
            this.editObj = {
                name: '',
                sort: '',
                icon: '',
                count_project_use: 0,
                count_task_use: 0,
                status: false,
            };
            this.handlePopShow();
        },
        editSubject(editIndex) {
            this.editObj = utils.simpleCopy(this.tableData[editIndex]);
            this.curIndex = editIndex;
            this.handleType = 'edit';
            this.handlePopShow();
        },
        initPopPar() {
            this.bid = 0;
            this.curIndex = 0;
            this.editObj = {};
        },
        cancelPop() {
            this.handlePopShow();
            this.initPopPar();
        },
        sumbitEdit() {
            let editObj = this.editObj;
            if (!editObj.name) {
                this.$message.error('未填写分类名称');
                return;
            }
            if (!editObj.icon) {
                this.$message.error('未上传图标');
                return;
            }
            this.$set(this.tableData,this.curIndex,editObj);
            this.handlePopShow();
            this.postEditTaskSubjectApi();
        },
        handleStatusChange(changeIndex) {
            this.curIndex = changeIndex;
            this.postEditTaskSubjectApi();
        },
        handleUploadSuccess(res) {
            let { code,data } = res;
            if (Number(code) === 0) {
                this.editObj.icon = data.url;
            }
        },
        searchWill() {
            this.getTaskSubjectListApi();
        },
        deleteSubject(delIndex) {
            console.log(delIndex);
            let changeObj =this.tableData[delIndex];
            this.deleteTaskSubjectApi(changeObj.id);
        },
        deleteTaskSubjectApi(id) {
            let _self = this;
            this.comResquest(this.request.deleteTaskSubject,(res)=>{
                _self.init();
                _self.$message({
                    message: '删除成功',
                    type: 'success'
                });
            },{
                id: id,
                load: true,
            })
        },
        imgShow(img) {
            if (img) {
                return img;
            }
            return '../../../../static/img/addImg2.png';
        },


        openAlias(){
          var _this = this;
          _this.aliasShow = true;
          _this.aliaLoading = true;
          _this.dynamicTags = [];
          fetch.get('/api/console/task/project/alias').then((res)=>{
          	let data = res.data;
          	if(data.code == 0){
              _this.dynamicTags = res.data.data;
          	}else{
          		_this.$message.error(data.message);
          	}
          }).catch((err)=>{
          	_this.$message.error(err.message);
          }).finally(()=>{
          	_this.aliaLoading = false;
          });
        },
        handleClose(tag) {
          var _this = this;
          _this.aliaLoading = true;
          fetch.get('/api/console/task/project/alias',{type:"DELETE",id:_this.dynamicTags[_this.dynamicTags.indexOf(tag)].id}).then((res)=>{
          	let data = res.data;
          	if(data.code == 0){
              _this.openAlias();
          	}else{
          		_this.$message.error(data.message);
          	}
          }).catch((err)=>{
          	_this.$message.error(err.message);
          }).finally(()=>{
          	_this.aliaLoading = false;
          });
        },
        showInput() {
          this.inputVisible = true;
          this.$nextTick(_=> {
            this.$refs.saveTagInput.$refs.input.focus();
          });
        },
        handleInputConfirm() {
          let inputValue = this.inputValue;
          let _this = this;
          if (inputValue) {
            _this.aliaLoading = true;
            fetch.post('/api/console/task/project/alias',{name:inputValue}).then((res)=>{
            	let data = res.data;
            	if(data.code == 0){
                _this.openAlias();
            	}else{
            		_this.$message.error(data.message);
            	}
            }).catch((err)=>{
            	_this.$message.error(err.message);
            }).finally(()=>{
            	_this.aliaLoading = false;
            });
          }
          this.inputVisible = false;
          this.inputValue = '';
        },
        changeAlias(index){
          var _this = this;
          //alert(index)
          _this.$prompt('请输入别名', '提示', {
            inputValue:_this.dynamicTags[index].name,
            confirmButtonText: '确定',
            cancelButtonText: '取消',
          }).then(({ value }) => {
            _this.aliaLoading = true;
            fetch.get('/api/console/task/project/alias',{type:"PUT",id:_this.dynamicTags[index].id,name:value}).then((res)=>{
            	let data = res.data;
            	if(data.code == 0){
                _this.openAlias();
            	}else{
            		_this.$message.error(data.message);
            	}
            }).catch((err)=>{
            	_this.$message.error(err.message);
            }).finally(()=>{
            	_this.aliaLoading = false;
            });
          });
        }
    },
}
